﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;
using TradeForm.Properties;
using System.Configuration;
using TradeForm.config;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Windows.Forms;

namespace TradeForm.Print
{
    public class ShipmentDocument : DocumentBase
    {
        //放置配置的字典
        protected Dictionary<string, Setting> dicPrintSets = new Dictionary<string, Setting>();

        public ShipmentDocument()
        { }
        public ShipmentDocument(string defcode)
        {
            this._defCode = defcode;
            getData(defcode);
            loadPrintSettings();

        }

        public ShipmentDocument(string defcode, bool isPrintBack)
        {
            this.IsPrintBack = isPrintBack;
            this._defCode = defcode;
            getData(defcode);
            loadPrintSettings();
        }

        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            g.PageUnit = GraphicsUnit.Millimeter;
            float fontHeight = g.MeasureString("字体测试高度", base.Font).Height;
            if (this.IsPrintBack)
            {
                if (File.Exists(Application.StartupPath + @"\BackImage\托运单.jpg"))
                {
                    g.DrawImage(Image.FromFile(Application.StartupPath + @"\BackImage\托运单.jpg"), 0, 0, Convert.ToSingle(e.PageBounds.Width * 25.4 / 100),
                   Convert.ToSingle(e.PageBounds.Height * 25.4 / 100));
                }

            }
            if (this.consb != null && this.goodsDetails != null && this.goodsDetails.Count > 0 && this.goodsSum != null)
            {

                g.DrawString(consb.Shipper, base.Font, Brushes.Black, dicPrintSets["Shipper"].PosX, dicPrintSets["Shipper"].PosY);
                g.DrawString(consb.Consignee, base.Font, Brushes.Black, dicPrintSets["Consignee"].PosX, dicPrintSets["Consignee"].PosY);
                g.DrawString(consb.NotifyParty, base.Font, Brushes.Black, dicPrintSets["NotifyParty"].PosX, dicPrintSets["NotifyParty"].PosY);

                g.DrawString(consb.DestPort, base.Font, Brushes.Black, dicPrintSets["ToPort"].PosX, dicPrintSets["ToPort"].PosY);
                g.DrawString(consb.FromPort, base.Font, Brushes.Black, dicPrintSets["FromPort"].PosX, dicPrintSets["FromPort"].PosY);
                g.DrawString(consb.FinalDestPort, base.Font, Brushes.Black, dicPrintSets["FinalDestPort"].PosX, dicPrintSets["FinalDestPort"].PosY);
                g.DrawString(consb.ShipmentDate.Value.ToString("yyyy-MM-dd"), base.Font, Brushes.Black, dicPrintSets["ShipmentDate"].PosX, dicPrintSets["ShipmentDate"].PosY);

                g.DrawString(consb.CanTranshipment.Value ? "YES" : "NO", base.Font, Brushes.Black, dicPrintSets["CanTransShipment"].PosX, dicPrintSets["CanTransShipment"].PosY);
                g.DrawString(consb.CanBatch.Value ? "YES" : "NO", base.Font, Brushes.Black, dicPrintSets["CanBatch"].PosX, dicPrintSets["CanBatch"].PosY);

                g.DrawString(consb.ValiDate.Value.ToString("yyyy-MM-dd"), base.Font, Brushes.Black, dicPrintSets["ValiDate"].PosX, dicPrintSets["ValiDate"].PosY);

                g.DrawString(consb.Mark, base.Font, Brushes.Black, dicPrintSets["Mark"].PosX, dicPrintSets["Mark"].PosY);
                g.DrawString(consb.TotalEName, base.Font, Brushes.Black, dicPrintSets["TotalEName"].PosX, dicPrintSets["TotalEName"].PosY);

                g.DrawString(goodsSum.TotalCount.Value.ToString("##0") + goodsSum.TotalCountUnit, base.Font, Brushes.Black, dicPrintSets["TotalCount"].PosX, dicPrintSets["TotalCount"].PosY);
                g.DrawString(goodsSum.TotalGrossWeight.Value.ToString("##0.00"), base.Font, Brushes.Black, dicPrintSets["TotalGrossWeight"].PosX, dicPrintSets["TotalGrossWeight"].PosY);
                g.DrawString(goodsSum.TotalMeas.Value.ToString("##0.000"), base.Font, Brushes.Black, dicPrintSets["TotalMeas"].PosX, dicPrintSets["TotalMeas"].PosY);
                // g.DrawString(consb.PaymentMethod + " " + consb.Freight, base.Font, Brushes.Black, dicPrintSets["Freight"].PosX, dicPrintSets["Freight"].PosY);
                g.DrawString(consb.PaymentMethod, base.Font, Brushes.Black, dicPrintSets["Freight"].PosX, dicPrintSets["Freight"].PosY);
                g.DrawString(goodsSum.TotalEName, base.Font, Brushes.Black, dicPrintSets["Remark"].PosX, dicPrintSets["Remark"].PosY);
                g.DrawString(consb.Remark, base.Font, Brushes.Black, dicPrintSets["Remark"].PosX, dicPrintSets["Remark"].PosY + 5.0f);
            }
            if (this._compny != null)
            {
                float widthCName = g.MeasureString(_compny.CName, base.TitleFont).Width;
                float widthEName = g.MeasureString(_compny.EName, base.TitleFont).Width;
                float widthEAddress = g.MeasureString(_compny.EAddress, base.Font).Width;
                float widthTel = g.MeasureString(_compny.Tel, base.Font).Width;

                g.DrawString(_compny.CName, base.TitleFont, Brushes.Black, Convert.ToSingle((210.0 - widthCName) / 2), dicPrintSets["CompanyCName"].PosY);
                g.DrawString(_compny.EName, base.TitleFont, Brushes.Black, Convert.ToSingle((210.0 - widthEName) / 2), dicPrintSets["CompanyEName"].PosY);
                g.DrawString(_compny.EAddress, base.Font, Brushes.Black, Convert.ToSingle((210.0 - widthEAddress) / 2), dicPrintSets["EAddress"].PosY);
                g.DrawString(_compny.Tel, base.Font, Brushes.Black, Convert.ToSingle((210.0 - widthTel) / 2), dicPrintSets["Tel"].PosY);

            }
        }

        protected void loadPrintSettings()
        {
            XElement printSettings = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + @"\config\ShipmentSettings.xml");
            IEnumerable<XElement> settings = null;
            settings = (from el in printSettings.Elements("setting")
                        select el);
            if (settings != null && settings.Count() > 0)
            {
                foreach (XElement item in settings)
                {
                    if (!dicPrintSets.ContainsKey(item.Attribute("name").Value))
                    {

                        Setting set = new Setting();
                        set.Name = item.Attribute("name").Value;
                        set.CName = item.Attribute("cname").Value;
                        set.PosX = Convert.ToSingle(item.Attribute("PosX").Value);
                        set.PosY = Convert.ToSingle(item.Attribute("PosY").Value);
                        dicPrintSets.Add(set.Name, set);
                    }
                    else
                    {
                        dicPrintSets[item.Attribute("name").Value].CName = item.Attribute("cname").Value;
                        dicPrintSets[item.Attribute("name").Value].PosX = Convert.ToSingle(item.Attribute("PosX").Value);
                        dicPrintSets[item.Attribute("name").Value].PosY = Convert.ToSingle(item.Attribute("PosY").Value);
                    }
                }

            }
            else
            {

            }

        }

    }
}
